package com.ruicar.afs.cloud.bizcommon.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinRentAdjustDetails;
import com.ruicar.afs.cloud.bizcommon.business.mapper.ApplyRentAdjustDetailsMapper;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyRentAdjustDetailsService;
import com.ruicar.afs.cloud.common.core.uid.UidGenerator;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 任意贷租金调整
 * @author: sijun.yu
 * @date: 2020/6/28 22:25
 */
@Service
@AllArgsConstructor
public class ApplyRentAdjustDetailsServiceImpl extends ServiceImpl<ApplyRentAdjustDetailsMapper, FinRentAdjustDetails> implements ApplyRentAdjustDetailsService {

    private UidGenerator uidGenerator;

    /**
     * 保存任意贷-调整租金
     *
     * @param inList
     * @return
     */
    public List<FinRentAdjustDetails> saveRentAdjustDetails(String applyNo, List<FinRentAdjustDetails> inList) {

        // 先删除
        List<FinRentAdjustDetails> rentList = this.getRentAdjustDetails(applyNo);
        if (rentList != null && rentList.size() > 0) {
            for (FinRentAdjustDetails details : rentList) {
                this.baseMapper.deleteById(details.getId());
            }
        }

        // 再保存
        List<FinRentAdjustDetails> resultList = new ArrayList<FinRentAdjustDetails>();
        if (inList != null && inList.size() > 0) {
            for (FinRentAdjustDetails details : inList) {
                FinRentAdjustDetails rentAdjustDetails = new FinRentAdjustDetails();
                rentAdjustDetails.setId(uidGenerator.getUID());
                rentAdjustDetails.setApplyNo(applyNo);
                rentAdjustDetails.setPeriod(details.getPeriod());
                rentAdjustDetails.setRent(details.getRent());
                this.baseMapper.insert(rentAdjustDetails);
                resultList.add(rentAdjustDetails);
            }
        }
        return resultList;
    }

    /**
     * 通过申请编号获取租金调整列表
     *
     * @param applyNo
     * @return
     */
    public List<FinRentAdjustDetails> getRentAdjustDetails(String applyNo) {

        List<FinRentAdjustDetails> list = this.baseMapper.selectList(Wrappers.<FinRentAdjustDetails>query().lambda()
                .eq(FinRentAdjustDetails::getApplyNo, applyNo));
        return list;
    }

}
